package com.ihome.android.market2.asynctask;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

import com.ihome.android.EAApp;
import com.ihome.android.academy.DetailActivity;
import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.market2.info.AppDetailInfo;
import com.ihome.android.market2.info.AppInfo;
import com.ihome.android.market2.view.DialogFactory;
import com.ihome.android.market2.view.MyProgressDialog;
import com.ihome.android.market2.view.DialogFactory.onBtnClickListener;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;
import com.ihome.android.util.log.UserBehaviorLog;
import com.ihome.android.util.log.UserBehaviorLog.NotifyType;
import com.ihome.android.util.log.UserBehaviorLog.UserBehavior;

/**
 * 获取应用详情和推荐应用
 * 
 * @author Emerson
 * 
 */
public class GetAppDetailTask extends AsyncTask<Bundle, Integer, Bundle> {
	private final String		TAG	= GetAppDetailTask.class.getSimpleName();
	private Context				context;
	private MyProgressDialog	progressDialog;
	private AppDetailInfo		mAppDetailInfo;
	private List<AppInfo>		mAppRelativeList;

	public GetAppDetailTask(Context context) {
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (!(context instanceof Activity))
			return;
		progressDialog = MyProgressDialog.getDialog(context, context.getString(R.string.user_grabing_data));
		progressDialog.show();
		progressDialog.setCancelable(true);
		progressDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (GetAppDetailTask.this.getStatus().compareTo(AsyncTask.Status.FINISHED) != 0)
					GetAppDetailTask.this.cancel(true);
			}
		});
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = params[0];
		int appId = b.getInt(HttpKeys.KEY_APPID);
		String pathId = b.getString(HttpKeys.KEY_PATHID);
		LogUtil.v(TAG, "appId = " + appId + "; pathId = " + pathId);
		if (appId <= 0) {
			return b;
		}

		try {
			String appinfo = new HttpActions(context).getAppInfo(appId, pathId);
			LogUtil.v(TAG, "appinfo = " + appinfo);
			mAppDetailInfo = JsonAnalytic.getInstance().anslyseAppDetailResult(appinfo);
			if (mAppDetailInfo != null) {
				LogUtil.v(TAG, "mAppDetailInfo =  \n " + mAppDetailInfo.toString());
			}
			else {
				cancel(true);
			}

			String apprelative = new HttpActions(context).getRelativeApp(appId, 0, 3);
			LogUtil.v(TAG, "apprelative = " + apprelative);
			mAppRelativeList = JsonAnalytic.getInstance().anslyseAppInfoResult(apprelative);
			LogUtil.v(TAG, "mAppRelativeList.size = " + mAppRelativeList.size());
		} catch (ITVException e) {
			String[] msg1 = { "", "", "" , "", "detail", "" };
			UserBehaviorLog.getInstance().writeLog(UserBehavior.STORERUN_EXCEPTION, NotifyType.TYPE_FAILURE, "", msg1);
			e.printStackTrace();
			LogUtil.e(TAG, e.toString());
			b.putInt(HttpKeys.KEY_CODE, e.getStatusCode());
			b.putString(HttpKeys.KEY_DESCRIPTION, e.getMessage());
		}
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);
		if (progressDialog != null)
			progressDialog.dismiss();

		/** 请求出错处理 */
		if (result.containsKey(HttpKeys.KEY_CODE)) {
//			if(ITVException.CODE_LOGINOUT_THREE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGINOUT_ONE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_SESSIONKEY == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGIN_ONE == result.getInt(HttpKeys.KEY_CODE)){
				new DialogFactory(context).showDialog("",result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")",context.getString(R.string.dialog_ok),new onBtnClickListener() {
					
					@Override
					public void btnRightClickListener(View v) {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void btnNeutralClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void btnLeftClickListener(View v) {
						// TODO Auto-generated method stub
						EAApp.getApp().exitApp(true);
					}
					
					@Override
					public void btnCloseClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
				});
				return;
			}
//			new DialogFactory(context).showDialog(result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")");
//			return;
//		}

		if (DetailActivity.class.isInstance(context) && mAppRelativeList != null) {
			// 填充AppRelativeList
			((DetailActivity) context).fillUpAppRelative(mAppRelativeList);
		}

		if (DetailActivity.class.isInstance(context) && mAppDetailInfo != null) {
			// 填充AppDetailInfo
			((DetailActivity) context).fillUpAppDetailInfo(mAppDetailInfo);
		}

	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");
		if (progressDialog != null)
			progressDialog.dismiss();

		if (context instanceof DetailActivity)
			((DetailActivity) context).finish();

	}

}
